cmake_minimum_required (VERSION 3.25)
project(GuoEngineThirdParty VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 17)

#set output file path add create it
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG 	${CMAKE_CURRENT_SOURCE_DIR}/build/Debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE 	${CMAKE_CURRENT_SOURCE_DIR}/build/Release)

execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG})
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE})


set(FinalOutPutDirectory ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty)
if(WIN32)
    set(PlatformType Win64)

elseif(LINUX)
    set(PlatformType Linux)
endif()


#make sure lib created
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

#add a target to generate all the third party lib
add_custom_target(ThirdPartyLib)


#zlib start***********************************************************************************


add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Libs/zlib)

#add -fPIC to zlib
set_target_properties(zlibstatic PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(zlib PROPERTIES POSITION_INDEPENDENT_CODE ON)

#set zlib install path
set(ZlibOutputDirDebug ${FinalOutPutDirectory}/zlib/${PlatformType}/Debug)
set(ZlibOutputDirRelease ${FinalOutPutDirectory}/zlib/${PlatformType}/Release)
set_target_properties(zlibstatic PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${ZlibOutputDirDebug})
set_target_properties(zlibstatic PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${ZlibOutputDirRelease})

#不知道为什么对动态库这么设置没用，得手动复制
set_target_properties(zlib PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${ZlibOutputDirDebug})
set_target_properties(zlib PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${ZlibOutputDirRelease})

#make zlib install path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${ZlibOutputDirDebug})
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${ZlibOutputDirRelease})
 
#make zlib include path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/zlib/include)

#copy zlib include file
execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/zlib/zlib.h ${FinalOutPutDirectory}/zlib/include)

foreach(zlib_head_file ${ZLIB_PUBLIC_HDRS})
    message(STATUS "copy ${zlib_head_file}")
    execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/zlib/${zlib_head_file} ${FinalOutPutDirectory}/zlib/include)
endforeach()

#add zlib to ThirdPartyLib
add_dependencies(ThirdPartyLib zlibstatic)
add_dependencies(ThirdPartyLib zlib)

add_custom_target(zlib_copy_head_file
    #copy zlib include file
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/zlib/zlib.h ${FinalOutPutDirectory}/zlib/include
    #copy zlib include file
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/build/Libs/zlib/zconf.h ${FinalOutPutDirectory}/zlib/include

    #手动复制zlib的动态库到输出目录
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:zlib> ${FinalOutPutDirectory}/zlib/${PlatformType}/$<CONFIG>

)

add_dependencies(zlib_copy_head_file zlibstatic) # make sure the target is built before copying ,because the zconf.h is generated by cmake
add_dependencies(ThirdPartyLib zlib_copy_head_file)



#zlib end***********************************************************************************


#libpng start***********************************************************************************

#set libpng build option
set(PNG_SHARED On CACHE BOOL "Build shared lib" FORCE) 
set(PNG_STATIC On CACHE BOOL "Build static lib" FORCE)
set(PNG_TESTS OFF CACHE BOOL "Build libpng tests" FORCE)
set(PNG_DEBUG OFF CACHE BOOL "Build libpng debug" FORCE)

#set(PNG_BUILD_ZLIB ON CACHE BOOL "Build with ZLIB" FORCE)

#skip install png
set(SKIP_INSTALL_LIBRARIES true)
set(SKIP_INSTALL_ALL true)

#give zlibstatic a alias name ZLIB::ZLIB ,so that libpng can find it
add_library(ZLIB::ZLIB ALIAS zlibstatic)
set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Libs/zlib)
set(ZLIB_LIBRARY zlibstatic)



add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Libs/libpng)

#how to add fpic to libpng
set_target_properties(png_shared PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(png_static PROPERTIES POSITION_INDEPENDENT_CODE ON)

#set libpng install path
# set_target_properties(png PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${FinalOutPutDirectory}/libpng/${PlatformType}/$<CONFIG>)
# set_target_properties(png PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${FinalOutPutDirectory}/libpng/${PlatformType}/$<CONFIG>)

#add  zlib include path to libpng
target_include_directories(png_shared PRIVATE ${FinalOutPutDirectory}/zlib/include)
target_include_directories(png_static PRIVATE ${FinalOutPutDirectory}/zlib/include)
#add  zlib lib path to libpng
target_link_directories(png_shared PRIVATE ${FinalOutPutDirectory}/zlib/${PlatformType}/$<CONFIG>)
target_link_directories(png_static PRIVATE ${FinalOutPutDirectory}/zlib/${PlatformType}/$<CONFIG>)
#add  zlib lib to libpng
#target_link_libraries(png PRIVATE zlibstatic)



#make libpng install path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/libpng/${PlatformType}/Debug)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/libpng/${PlatformType}/Release)

#make libpng include path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/libpng/include)

#copy libpng shared lib post build
add_custom_target(libpng_copy_lib_and_head
    #copy libpng  lib
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:png_shared> ${FinalOutPutDirectory}/libpng/${PlatformType}/$<CONFIG>
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:png_static> ${FinalOutPutDirectory}/libpng/${PlatformType}/$<CONFIG>

    #copy libpng include file
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/libpng/png.h ${FinalOutPutDirectory}/libpng/include
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/libpng/pngconf.h ${FinalOutPutDirectory}/libpng/include
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/build/Libs/libpng/pnglibconf.h ${FinalOutPutDirectory}/libpng/include
)
# add dependency to libpng_copy_lib_and_head
add_dependencies(libpng_copy_lib_and_head png_shared png_static)

#add libpng to ThirdPartyLib
add_dependencies(ThirdPartyLib png_shared png_static)
add_dependencies(ThirdPartyLib libpng_copy_lib_and_head)

#make sure zlib head file is exist build libpng
add_dependencies(png_shared zlibstatic zlib zlib_copy_head_file)
add_dependencies(png_static zlibstatic zlib zlib_copy_head_file)

#libpng end***********************************************************************************


#libjpeg begin********************************************************************************

#disable simd , because it is to complex to build 
set(WITH_SIMD OFF CACHE BOOL "Build with SIMD" FORCE)

#set doc install path (just for depress cmake error)
set(CMAKE_INSTALL_DOCDIR ${CMAKE_CURRENT_SOURCE_DIR}/build/libjpeg/doc)  



add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Libs/libjpeg)

#add -fPIC to libjpeg
set_target_properties(jpeg PROPERTIES POSITION_INDEPENDENT_CODE ON)
#set_target_properties(jpeg-static PROPERTIES POSITION_INDEPENDENT_CODE ON)

#set libjpeg install path,will auto copy import lib to install path
set_target_properties(jpeg PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${FinalOutPutDirectory}/libjpeg/${PlatformType}/$<CONFIG>)
set_target_properties(jpeg PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${FinalOutPutDirectory}/libjpeg/${PlatformType}/$<CONFIG>)

# set_target_properties(jpeg-static PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${FinalOutPutDirectory}/libjpeg/${PlatformType}/$<CONFIG>)
# set_target_properties(jpeg-static PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${FinalOutPutDirectory}/libjpeg/${PlatformType}/$<CONFIG>)


#add extra d to lib name
set_target_properties(jpeg PROPERTIES DEBUG_POSTFIX "d")
#set_target_properties(jpeg-static PROPERTIES DEBUG_POSTFIX "d")

#make libjpeg install path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/libjpeg/${PlatformType}/Debug)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/libjpeg/${PlatformType}/Release)

#make libjpeg include path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/libjpeg/include)


#copy libjpeg shared lib and head file post build
add_custom_target(libjpeg_copy_lib_and_head_file
    #copy libjpeg  lib
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:jpeg> ${FinalOutPutDirectory}/libjpeg/${PlatformType}/$<CONFIG>
    #COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:jpeg-static> ${FinalOutPutDirectory}/libjpeg/${PlatformType}/$<CONFIG>

    #copy libjpeg include file
    #COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/libjpeg/jmorecfg.h ${FinalOutPutDirectory}/libjpeg/include
    #COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/libjpeg/jpeglib.h ${FinalOutPutDirectory}/libjpeg/include
    #COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/libjpeg/jerror.h ${FinalOutPutDirectory}/libjpeg/include
    #COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Libs/libjpeg/jversion.h ${FinalOutPutDirectory}/libjpeg/include
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/build/Libs/libjpeg/jconfig.h ${FinalOutPutDirectory}/libjpeg/include
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/build/Libs/libjpeg/jpeglib.h ${FinalOutPutDirectory}/libjpeg/include
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/build/Libs/libjpeg/jerror.h ${FinalOutPutDirectory}/libjpeg/include
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/build/Libs/libjpeg/jmorecfg.h ${FinalOutPutDirectory}/libjpeg/include
    
    #COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/build/Libs/libjpeg/jconfigint.h ${FinalOutPutDirectory}/libjpeg/include
)

add_dependencies(libjpeg_copy_lib_and_head_file 
                jpeg 
                #jpeg-static
)

#add libjpeg to ThirdPartyLib
#add_dependencies(ThirdPartyLib jpeg jpeg-static)
add_dependencies(ThirdPartyLib libjpeg_copy_lib_and_head_file)


#libjpeg end***********************************************************************************



#lib freetype begin********************************************************************************

#set freetype build option
set(FT_DISABLE_ZLIB TRUE CACHE BOOL "" FORCE)
set(FT_DISABLE_BZIP2 TRUE CACHE BOOL "" FORCE)
set(FT_DISABLE_PNG TRUE CACHE BOOL "" FORCE)
set(FT_DISABLE_HARFBUZZ TRUE CACHE BOOL "" FORCE)
set(FT_DISABLE_BROTLI TRUE CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS FALSE)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Libs/freetype)

#add -fPIC to freetype
set_target_properties(freetype PROPERTIES POSITION_INDEPENDENT_CODE ON)

#set freetype install path
set(FreetypeOutputDirDebug ${FinalOutPutDirectory}/freetype/${PlatformType}/Debug)
set(FreetypeOutputDirRelease ${FinalOutPutDirectory}/freetype/${PlatformType}/Release)
set_target_properties(freetype PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${FreetypeOutputDirDebug})
set_target_properties(freetype PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${FreetypeOutputDirRelease})

#make freetype install path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FreetypeOutputDirDebug})
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FreetypeOutputDirRelease})

#make freetype include path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/freetype/include/)

#copy freetype shared lib and head file post build
add_custom_target(freetype_copy_shared_lib_and_head_file
    #copy freetype shared lib
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:freetype> ${FinalOutPutDirectory}/freetype/${PlatformType}/$<CONFIG>

    #copy freetype include file
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Libs/freetype/include ${FinalOutPutDirectory}/freetype/include
)


add_dependencies(freetype_copy_shared_lib_and_head_file freetype)

#add freetype to ThirdPartyLib
add_dependencies(ThirdPartyLib freetype_copy_shared_lib_and_head_file)


#lib freetype end********************************************************************************

#lib bullet3 begin********************************************************************************

set(BUILD_SHARED_LIBS FALSE)
file(GLOB_RECURSE AllLinearMathCppFiles ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/LinearMath/*.cpp ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/LinearMath/*.h )
file(GLOB_RECURSE AllCollsionCppFiles ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Collision/*.cpp ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Collision/*.h )
file(GLOB_RECURSE AllCommonCppFiles ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Common/*.cpp ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Common/*.h )
file(GLOB_RECURSE AllDynamicCppFiles ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Dynamics/*.cpp ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Dynamics/*.h )
file(GLOB_RECURSE AllGeometryCppFiles ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Geometry/*.cpp ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Geometry/*.h )
file(GLOB_RECURSE AllOlderCollsionCppFiles ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/BulletCollision/*.cpp ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/BulletCollision/*.h )
file(GLOB_RECURSE AllOlderDynamicsCppFiles ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/BulletDynamics/*.cpp ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/BulletDynamics/*.h )




add_library(bullet3 STATIC
    ${AllLinearMathCppFiles}
    ${AllCollsionCppFiles}
    ${AllCommonCppFiles}
    ${AllDynamicCppFiles}
    ${AllGeometryCppFiles}
    ${AllOlderCollsionCppFiles}
    ${AllOlderDynamicsCppFiles}
)
set(BUILD_UNIT_TESTS FALSE CACHE BOOL "" FORCE)
set(BUILD_EXTRAS FALSE CACHE BOOL "" FORCE)


add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3)

#add -fPIC to bullet3
set_target_properties(bullet3 PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_include_directories(bullet3 PRIVATE ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src)

#set bullet3 install path
set(Bullet3OutputDirDebug ${FinalOutPutDirectory}/bullet3/${PlatformType}/Debug)
set(Bullet3OutputDirRelease ${FinalOutPutDirectory}/bullet3/${PlatformType}/Release)
set_target_properties(bullet3 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${Bullet3OutputDirDebug})
set_target_properties(bullet3 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${Bullet3OutputDirRelease})

#make bullet3 install path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${Bullet3OutputDirDebug})
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${Bullet3OutputDirRelease})

#make bullet3 include path
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${FinalOutPutDirectory}/bullet3/include)


#copy bullet3 shared lib and head file post build
add_custom_target(bullet3_copy_shared_lib_and_head_file
    #copy bullet3 shared lib
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:bullet3> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>
    # COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:LinearMath> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>
    # COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:Bullet3Collision> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>
    # COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:Bullet3Common> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>
    # COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:Bullet3Dynamics> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>
    # COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:Bullet3Geometry> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>
    # COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:BulletCollision> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>
    # COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:BulletDynamics> ${FinalOutPutDirectory}/bullet3/${PlatformType}/$<CONFIG>


    #copy bullet3 include file
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Collision ${FinalOutPutDirectory}/bullet3/include/Bullet3Collision
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Common ${FinalOutPutDirectory}/bullet3/include/Bullet3Common
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Dynamics ${FinalOutPutDirectory}/bullet3/include/Bullet3Dynamics
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/Bullet3Geometry ${FinalOutPutDirectory}/bullet3/include/Bullet3Geometry
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/LinearMath ${FinalOutPutDirectory}/bullet3/include/LinearMath
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/BulletCollision ${FinalOutPutDirectory}/bullet3/include/BulletCollision
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/BulletDynamics ${FinalOutPutDirectory}/bullet3/include/BulletDynamics
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/btBulletCollisionCommon.h ${FinalOutPutDirectory}/bullet3/include
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/Libs/bullet3/src/btBulletDynamicsCommon.h ${FinalOutPutDirectory}/bullet3/include
)

add_dependencies(bullet3_copy_shared_lib_and_head_file bullet3)
#add_dependencies(ThirdPartyLib Bullet3Collision Bullet3Dynamics Bullet3Geometry BulletCollision LinearMath)

# #add bullet3 to ThirdPartyLib
add_dependencies(ThirdPartyLib bullet3_copy_shared_lib_and_head_file)


#lib bullet3 end********************************************************************************
